﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Wicture.QPZS.EPC.Core
{
    public class EpcEnum
    {
        #region 工具
        public static string GetName(System.Type t, object v)
        {
            try
            {
                return Enum.GetName(t, v);
            }
            catch
            {
                return "UNKNOWN";
            }
        }
        /// <summary>
        /// 返回指定枚举类型的指定值的描述
        /// </summary>
        /// <param name="t">枚举类型</param>
        /// <param name="v">枚举值</param>
        /// <returns></returns>
        public static string GetDescription(System.Type t, object v)
        {
            try
            {
                if (v == null)
                {
                    return string.Empty;
                }
                FieldInfo fi = t.GetField(GetName(t, v));
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                return (attributes.Length > 0) ? attributes[0].Description : GetName(t, v);
            }
            catch
            {
                return "UNKNOWN";
            }
        }

        public static SortedList GetStatus(System.Type t)
        {
            SortedList list = new SortedList();

            Array a = Enum.GetValues(t);
            for (int i = 0; i < a.Length; i++)
            {
                string enumName = a.GetValue(i).ToString();
                int enumKey = (int)System.Enum.Parse(t, enumName);
                string enumDescription = GetDescription(t, enumKey);
                list.Add(enumKey, enumDescription);
            }
            return list;
        }
        #endregion

        public enum ZpcServiceCode
        {
            [Description("vin解析")]
            VinQuery = 1,
            [Description("主组")]
            MainGroup = 2,
            [Description("子组")]
            SubGroup = 3,
            [Description("零件列表")]
            PartList = 4,
            [Description("零件详情")]
            PartDetail = 5,
            [Description("快速找件")]
            PartQuickQuery = 6,
            [Description("零件查询")]
            PartQuery = 7
        }

        public enum ZpcErrorCode
        {
            [Description("成功")]
            Ok = 200,
            [Description("未授权")]
            UnAuthorized = 401
        }

        public static string GetZpcErrorCode(object v)
        {
            return GetDescription(typeof(ZpcErrorCode), v);
        }
    }
}
